package cn.blue.pet.service.impl;

import cn.blue.pet.domain.Pet;
import cn.blue.pet.domain.PetType;
import cn.blue.pet.mapper.PetMapper;
import cn.blue.pet.mapper.PetTypeMapper;
import cn.blue.pet.service.IPetTypeService;
import cn.blue.base.service.impl.BaseServiceImpl;
import cn.blue.system.domain.Menu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hui
 * @since 2022-10-28
 */
@Service
public class PetTypeServiceImpl extends BaseServiceImpl<PetType> implements IPetTypeService {
    /**
     * 宠物类型树
     * @return
     */
@Autowired
  private PetTypeMapper petTypeMapper;
    @Override
    public List<PetType> type() {
        ArrayList<PetType> result = new ArrayList<>();
// 查询全部宠物类型
        List<PetType> petTypes = petTypeMapper.findAll();
// 转为map。id为key，对象为value
        Map<Long, PetType> map = petTypes.stream().collect(Collectors.toMap(PetType::getId, e -> e));
        for (PetType petType : petTypes) {
            if (petType.getPid() == null) {
                result.add(petType);
            } else {
                PetType parent = map.get(petType.getPid());
                if (parent != null) {
                    parent.getChildren().add(petType);
                }
            }
        }
        return result;
    }
}
